#pragma once
#include "stdafx.h"
#include "../QiangPiaoShenQi/INI1.26.h"
#include "upparam.h"

enum UpdatePriorit{
    UP_Pri_NoUpdate,
    UP_Pri_Suggestion,
    UP_Pri_Emergence
};

class CUpdateConfig{
public:
    CUpdateConfig(const std::wstring& inifname)
    {
        if (CFileHelper::IsFileExist(inifname))
        {
            success_ = ini_.Parse(CStringHelper::Unicode2ACSII(inifname));
        }
    }
    ~CUpdateConfig(void)
    {
        ini_.Save();
    }

    bool success(){return success_;}

    std::string GetVersion() { return ini_["APP"]["ver"]; }
    void SetVersion(const std::string& value) { SetValue("APP", "ver", value); }

private:
    void SetValue(const std::string& section_name, const std::string& name, const std::string& value)
    {
        if (ini_[section_name][name].compare(value) != 0)
        {
            ini_[section_name][name] = value;
        }
    }
private:
    bool     success_;
    INI<std::string, std::string, std::string> ini_;
};

class CUpdateGoTicket
{
public:
    CUpdateGoTicket(void);
    virtual ~CUpdateGoTicket(void);

    UpdatePriorit GetUpdatePriority(){return m_upPriority;}
    wstring GetUpdateServerConfig(const wstring&);
    bool NeedUpdate(const wstring& svrConfig);
    void ParseUpdateCfgInfo(const wstring& svrCfg);
    void SetUpdateTrigger(UpdateTrigger trg){m_upTrigger = trg;}
    UpdateTrigger GetUpdateTrigger() {return m_upTrigger; }
    bool DownloadUpdateFile();
    void RefreshVersionConfig();

private:
    bool UnzipAndRunUpdate(const unsigned char* dwnedData, size_t dataLen );
    wstring GetGoTickApplicationPath();
    void SetCurrentVersion(const string& ver);
    string GetCurrentVersion();
    string GetDefaultVersion();
    wstring GetDefaultConfigPath();

private:
    UpdatePriorit m_upPriority;
    UpdateTrigger m_upTrigger;
    string        m_strlatestVersion;
    string        m_strDesc;
    vector<string> m_vecDwnUrls;
};

